using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
using System.Drawing.Design;
using System.Drawing;
using System.Globalization;
using System.ComponentModel.Design.Serialization;
using System.Reflection;

namespace SCS.Web.Controls.Design
{
	internal class ChildMenuMarkerConverter : ExpandableObjectConverter
	{
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type t)
		{
			if (t == typeof(string))
				return true;

			return base.CanConvertFrom(context, t);
		}

		public override bool CanConvertTo(ITypeDescriptorContext context, Type destType) 
		{
			if (destType == typeof(InstanceDescriptor) || destType == typeof(string)) 
			{
				return true;
			}
			return base.CanConvertTo(context, destType);
		}	
		
		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo info, object value)
		{
			if (value is string)
			{
				string str = (string)value;
					
				try
				{
					string[] pairs = str.Split(';');
					string[] args = new string[5];

					foreach (object pair in pairs)
					{
						string name = ((string)pair).Split(':')[0];
						string val = ((string)pair).Split(':')[1];

						switch (name)
						{
							case "N":
								args[0] = val;
								break;

							case "O":
								args[1] = val;
								break;

							case "S":
								args[2] = val;
								break;

							case "SO":
								args[3] = val;
								break;

							case "D":
								args[4] = val;
								break;
						}
					}

					return new ChildMenuMarker(args[0], args[1], args[2], args[3], args[4]);
				}
				catch
				{
					return new ChildMenuMarker();
				}				
			}
			return base.ConvertFrom(context,info,value);
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo info, object value, Type destType )
		{
			if (destType == typeof(string))
			{
				ChildMenuMarker marker = (ChildMenuMarker)value;
				
				string text = string.Empty;

				if (marker.Image.Url.Length > 0)
					text += string.Format(";", marker.Image.ToString());
				
				if (marker.ImageOver.Url.Length > 0)
					text += string.Format(";", marker.ImageOver.ToString());
				
				if (marker.ImageSelected.Url.Length > 0)
					text += string.Format(";", marker.ImageSelected.ToString());
				
				if (marker.ImageSelectedOver.Url.Length > 0)
					text += string.Format(";", marker.ImageSelectedOver.ToString());
				
				if (marker.ImageDisabled.Url.Length > 0)
					text += string.Format(";", marker.ImageDisabled.ToString());
				
				if (text.Length > 0)
					text = text.Substring(1);

				return text;
			}
			else if (destType == typeof(InstanceDescriptor)) 
			{
				Type[] types = new Type[] { typeof(string), typeof(string), typeof(string), typeof(string) };
				ConstructorInfo constructorInfo = typeof(ChildMenuMarker).GetConstructor(types);
				
				ChildMenuMarker style = (ChildMenuMarker)value;
				object[] args = new object[] { style.Normal, style.Over, style.Selected, style.SelectedOver };

				InstanceDescriptor descriptor = new InstanceDescriptor(constructorInfo, args, true);

				return descriptor;
			}
			return base.ConvertTo(context, info, value, destType);
		}
	}
}
